package net.pws.common.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

public class NumberUtils {
    public static final BigDecimal ZERO_BIGDECIMAL = new BigDecimal("0");
    
    public static interface AccumlateCallback {
        public BigDecimal getBigDecimal(Object target);
    }
    
    public static BigDecimal accumlate(BigDecimal[] targets) {
        BigDecimal result = new BigDecimal("0");
        for (int i = 0; i < targets.length; i++) {
            BigDecimal target = targets[i];
            if (target != null) {
                result = result.add(target);
            }
        }
        return result;
    }
    
    @SuppressWarnings("rawtypes")
	public static BigDecimal accumlate(List targets,
                                       AccumlateCallback bigDecimalLookup) {
        BigDecimal result = new BigDecimal("0");
        for (Iterator iterator = targets.iterator(); iterator.hasNext();) {
            Object target = (Object) iterator.next();
            BigDecimal bigDecimalOfTarge = bigDecimalLookup.getBigDecimal(target);
            if (bigDecimalOfTarge != null) {
                result = result.add(bigDecimalOfTarge);
            }
        }
        return result;
    }
    
    /**
     * 格式化资金数据，保留小数后两位，在单位基数为元的时候调用该方法进行格式化
     * 
     * @param source
     *            资金数，基数为：元
     */
    public static BigDecimal formatBigDecimalTo2(BigDecimal source) {
        source = source == null ? new BigDecimal("0") : source;
        DecimalFormat nformat = new DecimalFormat("0.00");
        return new BigDecimal(nformat.format(source));
    }
    
    /**
     * 由元转化成万元 格式化资金数据，保留小数后4位，在单位基数为元的时候调用该方法进行格式化
     * 
     * @param source
     *            资金数，基数为：元
     */
    public static BigDecimal formatBigDecimalToSix(BigDecimal source) {
        source = source == null ? new BigDecimal("0") : source;
        DecimalFormat nformat = new DecimalFormat("0.000000");
        String temp = nformat.format(source.divide(new BigDecimal("10000"),
                                                   6,
                                                   BigDecimal.ROUND_HALF_UP));
        return new BigDecimal(temp);
    }
    
    /**
     * 由万元转化成元，并且保留小数位后两位
     * 
     * @param source
     *            资金数，基数为：万元
     * @return
     */
    public static BigDecimal formatBigDecimal(BigDecimal source) {
        source = source == null ? new BigDecimal("0") : source;
        DecimalFormat nformat = new DecimalFormat("0.00");
        String temp = nformat.format(source.multiply(new BigDecimal("10000")));
        return new BigDecimal(temp);
    }
    
    /**
     * 由元转化成万元 格式化资金数据，保留小数后6位，在单位基数为元的时候调用该方法进行格式化
     * 
     * @param source
     *            资金数，基数为：元
     */
    public static BigDecimal formatBigDecimalTo6(BigDecimal source) {
        source = source == null ? new BigDecimal("0") : source;
        DecimalFormat nformat = new DecimalFormat("0.000000");
        String temp = nformat.format(source.divide(new BigDecimal("10000"),
                                                   6,
                                                   BigDecimal.ROUND_HALF_UP));
        return new BigDecimal(temp);
    }
    
    /**
     * 入帐如果为负，则为0
     * 
     * @param invest
     * @return
     */
    // RTMS00035700 负入帐转化为0 begin
    public static BigDecimal postInvestRoles(BigDecimal invest) {
        invest = invest == null ? new BigDecimal("0") : invest;
        invest = invest.compareTo(new BigDecimal("0")) < 0 ? new BigDecimal("0")
                                                          : invest;
        return invest;
    }
    
    // RTMS00035700 end
    
    /**
     * 将小数的权重化为2位数，并返回字符串
     */
    public static String formatProgressAsString(BigDecimal progress) {
        progress = progress == null ? new BigDecimal("0") : progress;
        DecimalFormat nformat = new DecimalFormat("0.00");
        String temp = nformat.format(progress.multiply(new BigDecimal("100")));
        return temp;
    }
    
    /**
     * 格式化Double，保留小数后两位
     * 
     * @param source
     *            源数
     */
    public static String formatDoubleTo2(Double source) {
        DecimalFormat format = new DecimalFormat("0.00");
        return format.format(source.doubleValue());
    }
    
    /**
     * 格式化Double，保留小数后六位
     * 
     * @param source
     *            源数
     */
    public static String formatDoubleTo6(Object source) {
    	if(source == null) {
    		return "";
    	}
    	
    	DecimalFormat format = new DecimalFormat("0.######");
    	if(source instanceof Number) {
    		Number n = (Number) source;
    		return format.format(n.doubleValue());
    	} else {
    		String s = source.toString();
    		if(org.apache.commons.lang.math.NumberUtils.isNumber(s)){
    			return format.format(Double.parseDouble(s));
    		}
    	}
    	
    	return "";
    }
         
    /**
     * @param longsInString
     * @return
     */
    public static Long[] parseLongs(String longsInString) {
        if (StringUtils.isEmpty(longsInString)) {
            return ArrayUtils.EMPTY_LONG_OBJECT_ARRAY;
        }
        
        if (longsInString.indexOf(",") == -1) {
            return new Long[] { Long.valueOf(longsInString) };
        }
        
        String[] numbersStringArray = longsInString.split(",");
        Long[] result = new Long[numbersStringArray.length];
        
        for (int i = 0; i < numbersStringArray.length; i++) {
            result[i] = Long.valueOf(numbersStringArray[i]);
        }
        
        return result;
    }
    
    public static Long[] convert2Long(String[] longInStrings) {
        int length = longInStrings.length;
        Long[] result = new Long[length];
        for (int i = 0; i < length; i++) {
            if (StringUtils.isNotEmpty(longInStrings[i])) {
                result[i] = Long.parseLong(longInStrings[i]);
            }
            else {
                result[i] = 0l;
            }
            
        }
        return result;
    }
    
    public static BigDecimal[] convert2BigDecimal(String[] bigDecimalInStrings) {
        int length = bigDecimalInStrings.length;
        BigDecimal[] result = new BigDecimal[length];
        for (int i = 0; i < length; i++) {
            if (StringUtils.isNotEmpty(bigDecimalInStrings[i])) {
                result[i] = new BigDecimal(bigDecimalInStrings[i]);
            }
            else {
                ;
                result[i] = new BigDecimal(0);
            }
            
        }
        return result;
        
    }
    
    public static boolean isNull(BigDecimal number) {
        return null == number || number.intValue() == 0;
    }
    
    public static boolean isNotNull(BigDecimal number) {
        return !isNull(number);
    }
    
}
